package com.lhwl.it.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class RequestMap  extends Pmap {
	private String sflag="0";//是否查询，查询则刷新查询条件
	
	public RequestMap() {
		super();
	}
	
	public RequestMap(HttpServletRequest request) {
		HttpSession session=request.getSession();
		
		Map map=request.getParameterMap();
		putAll(map);
		
		sflag=getStr("sflag");
		
		if (sflag!=null&&sflag.equals("1")) {
			remove(request,session,"sp");
			put(request,session,"sp");
		}
		putpage(request,session);
		put(session,"sp");
	}
	
	@Override
	public Object get(Object key) {
		Object value=super.get(key);
		if (value instanceof Object[] ) {
			Object[] oarray=(Object[])value;
			if (oarray.length==1) {
				return oarray[0];
			}		
		}
		return value;		
	}
	
	
	public String[] getValues(Object key) {
		String[] values=(String[])super.get(key);
		return values;
	}
	
	public String getStr(Object key) {
		Object temps=get(key);
		if (temps != null) {
			return temps.toString();
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	protected void remove(HttpServletRequest request,HttpSession session,String index) {		
		Enumeration<String> satrs=(Enumeration<String>)session.getAttributeNames();
		
		ArrayList<String> keylist=(ArrayList<String>)Collections.list(satrs);

		for (int i = 0;i < keylist.size() ; i++ ) {
			String key=keylist.get(i);
			if (key.startsWith(index)||key.startsWith(Pconstants.PAGECURPAGE)) {
				session.removeAttribute(key);
			}
		}
	}
		
	@SuppressWarnings("unchecked")
	protected void put(HttpServletRequest request,HttpSession session,String index) {		
		Enumeration<String> satrs=(Enumeration<String>)request.getParameterNames();
		
		/*
		ArrayList<String> keylist=new ArrayList<String>();
		
		while(satrs.hasMoreElements()){
			String keys=satrs.nextElement();
			if (keys.startsWith(index)||keys.startsWith(Pconstants.PAGECURPAGE)) {
				keylist.add(keys);
			}
		}
		*/
		
		ArrayList<String> keylist=(ArrayList<String>)Collections.list(satrs);
		
		for (int i = 0;i < keylist.size() ; i++ ) {
			String key=keylist.get(i);
			
			if (key.startsWith(index)||key.startsWith(Pconstants.PAGECURPAGE)) { 
				//session.removeAttribute(key);
				String newvalue=request.getParameter(key);
				if (!DataUtil.trim(newvalue).equals("")) {
					//System.out.println("Has put :"+key+"	"+newvalue);
					session.setAttribute(key, newvalue);
				}
			}
		}
		
	}
	
	
	@SuppressWarnings("unchecked")
	protected void put(HttpSession session,String index) {		
		Enumeration<String> satrs=(Enumeration<String>)session.getAttributeNames();
		
		ArrayList<String> keylist=new ArrayList<String>();
		
		while(satrs.hasMoreElements()){
			String keys=satrs.nextElement();
			if (keys.startsWith(index)||keys.startsWith(Pconstants.PAGECURPAGE)) {
				keylist.add(keys);
				Object  value=session.getAttribute(keys);
				put(keys,value);
			}
		}	
	}
	
	@SuppressWarnings("unchecked")
	protected  void putpage(HttpServletRequest request,HttpSession session) {
		String pagev=request.getParameter(Pconstants.PAGECURPAGE);
		if (!DataUtil.trim(pagev).equals("")) {
			session.setAttribute(Pconstants.PAGECURPAGE, pagev);
		}
	}

}
